﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//以上几句是自动生成的

using System.IO.Ports;//我加的，包含串口相关的
using System.Text.RegularExpressions;//我加的


namespace tempsensor
{
    public partial class Form1 : Form
    {
        private SerialPort comm = new SerialPort();//我加的，新建一个串口变量
        private StringBuilder builder = new StringBuilder();//避免在事件处理方法中反复的创建，定义到外面。  
        private bool automode;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();
            Array.Sort(ports);
            comboPortName.Items.AddRange(ports);
            comboPortName.SelectedIndex = comboPortName.Items.Count > 0 ? 0 : -1;

            //初始化SerialPort对象  
            comm.NewLine = "/r/n";
            comm.RtsEnable = true;//根据实际情况吧。 
            comm.DataReceived += comm_DataReceived;

            automode = true;
        }

        private void comboPortName_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void buttonOpenCloseCom_Click(object sender, EventArgs e)
        {
            //根据当前串口对象，来判断操作  
            if (comm.IsOpen)
            {
                comm.Close();
            }
            else
            {
                //关闭时点击，则设置好端口，波特率后打开  
                comm.PortName = comboPortName.Text;
                comm.BaudRate = 115200;
                try
                {
                    comm.Open();
                    
                }
                catch (Exception ex)
                {
                    //捕获到异常信息，创建一个新的comm对象，之前的不能用了。  
                    comm = new SerialPort();
                    //现实异常信息给客户。  
                    MessageBox.Show(ex.Message);
                }
            }
            //设置按钮的状态  
            buttonOpenCloseCom.Text = comm.IsOpen ? "关闭串口" : "打开串口";
        }

        void comm_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            int n = comm.BytesToRead;//先记录下来，避免某种原因，人为的原因，操作几次之间时间长，缓存不一致
            byte[] buf = new byte[n];//声明一个临时数组存储当前来的串口数据
            comm.Read(buf, 0, n);//读取缓冲数据
            byte[] SendBuf = new byte[2];
            //int temp = Convert.ToInt16(Encoding.ASCII.GetString(buf));
            //因为要访问ui资源，所以需要使用invoke方式同步ui。
            this.Invoke((EventHandler)(delegate
            {
              
                        this.WenDuZhi.Text = (buf[3] - 48).ToString() + (buf[4] - 48).ToString();//显示在节点1温度文本框内
                        if(((buf[3] - 48)*10 + (buf[4] - 48))> Convert.ToInt16(wenduyuzhi.Text))
                        {
                            wendubaojing.BackColor = Color.Red;
                        }
                        else
                        {
                            wendubaojing.BackColor = Color.Green;
                        }
                        this.shiduzhi.Text = (buf[5] - 48).ToString() + (buf[6] - 48).ToString();//显示在节点1温度文本框内
                        if (((buf[5] - 48) * 10 + (buf[6] - 48)) > Convert.ToInt16(shiduyuzhi.Text))
                        {
                            shidubaojing.BackColor = Color.Red;
                        }
                        else
                        {
                            shidubaojing.BackColor = Color.Green;
                        }
                     
                   
                        this.yanwuzhi.Text = (buf[0] - 48).ToString() + (buf[1] - 48).ToString() + (buf[2] - 48).ToString(); //在节点1温度文本框内
                        if (automode == true)
                        {
                            if (((buf[0] - 48) * 100 + (buf[1] - 48) * 10 + (buf[2] - 48)) > Convert.ToInt16(yanwuyuzhi.Text))
                            {
                                yanwubaojing.BackColor = Color.Red;
                                axWindowsMediaPlayer1.URL = "C:\\Users\\lenovo\\Music\\123.mp3";

                                buttonCloseLight_Click(null, null);
                            }
                            else
                            {
                                yanwubaojing.BackColor = Color.Green;
                                buttonOpenLight_click(null, null);

                            }
                        }
                        else {
                            if (((buf[0] - 48) * 100 + (buf[1] - 48) * 10 + (buf[2] - 48)) > Convert.ToInt16(yanwuyuzhi.Text))
                            {
                                yanwubaojing.BackColor = Color.Red;
                                axWindowsMediaPlayer1.URL = "C:\\Users\\lenovo\\Music\\123.mp3";

                              
                            }
                            else
                            {
                                yanwubaojing.BackColor = Color.Green;
                               

                            }
                        }

                        
                        this.guangqiangzhi.Text = (buf[7] - 48).ToString() + (buf[8] - 48).ToString();//显示在节点1温度文本框内
                        if (((buf[7] - 48) * 10 + (buf[8] - 48)) > Convert.ToInt16(guangqiangyuzhi.Text))
                        {
                            guangzhaobaojing.BackColor = Color.Red;
                        }
                        else
                        {
                            guangzhaobaojing.BackColor = Color.Green;
                        }
                    
                   
                        this.hongwaizhi.Text = (buf[9] - 48).ToString();//显示在节点1温度文本框内
                        if ((buf[9] - 48) ==1)
                        {
                            DaoZeiBaoJing.BackColor = Color.Red;
                            DaoZeiBaoJing.Text = "有人";
                        }
                        else
                        {
                            DaoZeiBaoJing.BackColor = Color.Green;
                            DaoZeiBaoJing.Text = "无人";
                        }
                      
                
                //if (automode == true)
                //{
                //    if (temp != null)
                //    {
                //        if (temp > Convert.ToInt16(wenduyuzhi.Text))
                //        {
                //            buttonCloseLight_Click(null, null);
                //        }
                //        else
                //        {
                //            buttonOpenLight_click(null, null);
                //        }
                //    }
                //    else
                //    {
                //        wenduyuzhi.Text = "未输入阈值";
                //    }

                //}
            }));
        }

        private void WenDuZhi_TextChanged(object sender, EventArgs e)
        {

        }


        //发送20到串口，继电器闭合
        private void buttonCloseLight_Click(object sender, EventArgs e)
        {
            byte[] SendBuf = new byte[2];
            SendBuf[0] = 0x32;
            SendBuf[1] = 0x31;
            comm.Write(SendBuf, 0, 2);
            System.Threading.Thread.Sleep(10);
        }
        //发送21到串口，继电器断开
        private void buttonOpenLight_click(object sender, EventArgs e)
        {
            byte[] SendBuf = new byte[2];
            SendBuf[0] = 0x32;
            SendBuf[1] = 0x30;
            comm.Write(SendBuf, 0, 2);
            System.Threading.Thread.Sleep(10);
            
        }

        private void WenDuZhi_TextChanged_1(object sender, EventArgs e)
        {

        }

        private void wenduyuzhi_TextChanged(object sender, EventArgs e)
        {

        }
        //按下automode赋值false，手动
        private void manu_Click(object sender, EventArgs e)
        {
            automode = false;
        }

        //按下automode赋值true，自动
        private void auto_Click(object sender, EventArgs e)
        {
            automode = true;
        }

        private void label8_Click(object sender, EventArgs e)
        {

        }

        private void DaoZeiBaoJing_Click(object sender, EventArgs e)
        {

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void shiduyuzhi_TextChanged(object sender, EventArgs e)
        {

        }

        private void yanwuyuzhi_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
